package com.itxiuyixiu.volunteer.controller;

import com.itxiuyixiu.entity.model.*;
import com.itxiuyixiu.tools.util.returns.ReturnVo;
import com.itxiuyixiu.volunteer.common.errors.ServiceException;
import com.itxiuyixiu.volunteer.dto.StudentAuthenticationParam;
import com.itxiuyixiu.volunteer.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "用户(学生)API")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "学生验证身份", response = String.class)
    @PostMapping("/student/verification")
    public ReturnVo validateStudent(@Valid StudentAuthenticationParam student, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ReturnVo.getFailureReturn(bindingResult);
        }
        try {
            userService.validateStudent(student);
            return ReturnVo.getSuccessReturn("学生身份验证通过");
        } catch (ServiceException e) {
            return ReturnVo.getFailureReturn(e.getMessage());
        }
    }

    @ApiOperation(value = "获取所有志愿者组织信息", response = Organization.class, responseContainer = "List")
    @GetMapping("/organizations")
    public ReturnVo getAllOrganization() {
        return ReturnVo.getSuccessReturn(userService.findAllOrganization());
    }

    @ApiOperation(value = "获取指定志愿者组织的信息", response = Organization.class)
    @GetMapping("/organization/{OrgId}")
    public ReturnVo getOrganizationByOrgId(@PathVariable("OrgId") Integer OrgId) {
        Organization organization = userService.findOrganizationByOrgId(OrgId);
        if (organization == null) {
            return ReturnVo.getFailureReturn("该志愿者组织不存在！");
        }
        return ReturnVo.getSuccessReturn(organization);
    }

    @ApiOperation(value = "查看指定组织包含的所有志愿者项目", response = Organization.class)
    @GetMapping("/projects/{OrgId}")
    public ReturnVo findOrganizationWithProjectByOrganizationId(@PathVariable("OrgId") Integer OrgId) {
        return ReturnVo.getSuccessReturn(userService.findOrganizationWithProjectByOrganizationId(OrgId));
    }

    @ApiOperation(value = "获取指定组织id下指定项目id的信息", response = Project.class)
    @GetMapping("/project/{projectId}")
    public ReturnVo findProjectWithOrganizationAndActivityByProjectId(@PathVariable("projectId") Integer projectId) {
        Project project = userService.findProjectWithOrganizationAndActivityByProjectId(projectId);
        if (project == null) {
            return ReturnVo.getFailureReturn("该项目不存在！");
        }
        return ReturnVo.getSuccessReturn(project);
    }

    @ApiOperation(value = "获取已发布的活动信息", response = ActivityView.class, responseContainer = "List")
    @GetMapping("/activities")
    public ReturnVo getAllActivity() {
        return ReturnVo.getSuccessReturn(userService.findAllActivity());
    }

    @ApiOperation(value = "获取某志愿者组织下的所有活动", response = Organization.class)
    @GetMapping("/activities/{orgId}")
    public ReturnVo findOrganizationWithActivityByOrganizationId(@PathVariable("orgId") Integer orgId) {
        Organization organization = userService.findOrganizationWithActivityByOrganizationId(orgId);
        if (organization == null) {
            return ReturnVo.getFailureReturn("该活动不存在！");
        }
        return ReturnVo.getSuccessReturn(organization);
    }

    @ApiOperation(value = "查看指定id的活动", response = Activity.class)
    @GetMapping("/activity/{activityId}")
    public ReturnVo getActivityByActivityId(@PathVariable("activityId") Integer activityId) {
        try {
            return ReturnVo.getSuccessReturn(userService.findActivityWithProjectAndOrganizationByActivityId(activityId));
        } catch (ServiceException e) {
            return ReturnVo.getFailureReturn("该活动不存在！");
        }
    }

    @ApiOperation(value = "参加活动", response = String.class)
    @PostMapping("/activity/{activityId}")
    public ReturnVo enterActivity(@PathVariable("activityId") Integer activityId) {
        try {
            userService.enterActivity(activityId);
            return ReturnVo.getSuccessReturn("成功参加活动");
        } catch (Exception e) {
            return ReturnVo.getFailureReturn(e.getMessage());
        }
    }

    @ApiOperation(value = "退出活动", response = String.class)
    @DeleteMapping("/activity/{activityId}")
    public ReturnVo exitActivity(@PathVariable("activityId") Integer activityId) {
        try {
            userService.exitActivity(activityId);
            return ReturnVo.getSuccessReturn("成功退出活动");
        } catch (Exception e) {
            return ReturnVo.getFailureReturn(e.getMessage());
        }
    }

    @ApiOperation(value = "查看参加活动信息", response = EnterListView.class, responseContainer = "List")
    @GetMapping("/state/activity/{activityId}")
    public ReturnVo findEnterActivityState(@PathVariable("activityId") Integer activityId) {
        try {
            return ReturnVo.getSuccessReturn(userService.findEnterActivityState(activityId));
        } catch (ServiceException e) {
            return ReturnVo.getFailureReturn(e.getMessage());
        }
    }


    @ApiOperation(value = "获取已报名活动信息", response = EnterListView.class, responseContainer = "List")
    @GetMapping("/enters")
    public ReturnVo getEnters() {
        try {
            return ReturnVo.getSuccessReturn(userService.findEnterActivity());
        } catch (ServiceException e) {
            return ReturnVo.getFailureReturn(e.getMessage());
        }
    }
}
